//
//  OrderInterface.m
//  DaJiang
//
//  Created by sally on 16/10/12.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "OrderInterface.h"

@implementation OrderInterface

- (id)init {
  self = [super initWithPath:@"/pro_topic"];
  if (self) {
  }
  return self;
}

#pragma mark - 约见接口相关
#pragma mark - 普通用户端
//预约专家的一个话题
- (void)bookOrderWithProTopicId:(NSInteger)proTopicId
                   proTopicName:(NSString *)proTopicName
                  proTopicPrice:(CGFloat)proTopicPrice
                         proUid:(NSInteger)proUid
                        problem:(NSString *)problem
                          intro:(NSString *)intro
                         proPic:(NSString *)proPic
                        proName:(NSString *)proName
                       proTitle:(NSString *)proTitle
                     completion:(completion_t)completion
                          error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"book.json");
  UserModel *userModel = [[CacheManager shareInstance] getLastLoginAccount];

  NSDictionary *parameters = @{ @"pro_topic_id" : @(proTopicId),
                                @"pro_topic_name" : proTopicName,
                                @"pro_topic_price" : @(proTopicPrice),
                                @"uid" : @(userModel.uid),
                                @"pro_uid" : @(proUid),
                                @"consulting_problem" : problem,
                                @"self_intro" : intro,
                                @"pro_user_profile_pic" : proPic,
                                @"pro_user_name" : proName,
                                @"pro_user_pro_title" : proTitle,
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//确认付费
- (void)surePayedWithConsultId:(NSInteger)consultId
                        proUid:(NSInteger)proUid
                     cashValue:(CGFloat)cashValue
                     topicName:(NSString *)topicName
                    completion:(completion_t)completion
                         error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"payed.json");
  UserModel *userModel = [[CacheManager shareInstance] getLastLoginAccount];

  NSDictionary *parameters = @{ @"consult_id" : @(consultId),
                                @"uid" : @(userModel.uid),
                                @"pro_uid" : @(proUid),
                                @"cash_value" : @(cashValue),
                                @"topic_name" : topicName,
                                @"user_name" : userModel.user_data.user_name,
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//用户取消约见
- (void)userCancelOrderWithReason:(NSString *)reason
                        consultId:(NSInteger)consultId
                       completion:(completion_t)completion
                            error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"user_cancel.json");

  NSDictionary *parameters = @{ @"cancelled_reason" : reason,
                                @"consult_id" : @(consultId),
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//提醒专家
- (void)remindExpertWithProUid:(NSInteger)proUid
                     consultId:(NSInteger)consultId
                    completion:(completion_t)completion
                         error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"remind_pro_user.json");
  UserModel *userModel = [[CacheManager shareInstance] getLastLoginAccount];

  NSDictionary *parameters = @{ @"pro_uid" : @(proUid),
                                @"uid" : @(userModel.uid),
                                @"user_name" : userModel.user_data.user_name,
                                @"consult_id" : @(consultId),
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//评价专家
- (void)commentExpertWithComment:(NSString *)comment
                       topicName:(NSString *)topicName
                          proUid:(NSInteger)proUid
                        cmtValue:(NSInteger)cmtValue
                         topicId:(NSInteger)topicId
                       consultId:(NSInteger)consultId
                      completion:(completion_t)completion
                           error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"comment.json");
  UserModel *userModel = [[CacheManager shareInstance] getLastLoginAccount];

  NSDictionary *parameters = @{ @"comment" : comment,
                                @"pro_topic_name" : topicName,
                                @"pro_uid" : @(proUid),
                                @"uid" : @(userModel.uid),
                                @"cmt_value" : @(cmtValue),
                                @"pro_topic_id" : @(topicId),
                                @"consult_id" : @(consultId),
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

#pragma mark - 专家端
//专家取消约见
- (void)expertCancelOrderWithConsultId:(NSInteger)consultId
                            completion:(completion_t)completion
                                 error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"pro_user_refuse.json");

  NSDictionary *parameters = @{ @"consult_id" : @(consultId),
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//专家接受约见
- (void)expertConfirmOrderWithConsultId:(NSInteger)consultId
                             completion:(completion_t)completion
                                  error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"confirm.json");

  NSDictionary *parameters = @{ @"consult_id" : @(consultId),
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//用户失约
- (void)userMissedMeetWithConsultId:(NSInteger)consultId
                         completion:(completion_t)completion
                              error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"user_missed_meet.json");

  NSDictionary *parameters = @{ @"consult_id" : @(consultId),
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//专家确认已经见面
- (void)hasBeenMeetWithConsultId:(NSInteger)consultId
                      completion:(completion_t)completion
                           error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"has_been_met.json");

  NSDictionary *parameters = @{ @"consult_id" : @(consultId),
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

#pragma mark - 我的（专家）相关
//新增修改话题
- (void)addOrUpdateTopicWithTopicId:(NSInteger)topicId
                          topicName:(NSString *)topicName
                         timeLength:(NSString *)timeLength
                          topicDesc:(NSString *)topicDesc
                         topicPrice:(NSString *)topicPrice
                         completion:(completion_t)completion
                              error:(error_t)error {
  UserModel *userModel = [[CacheManager shareInstance] getLastLoginAccount];
  NSURL *url;
  NSDictionary *parameters;
  if (topicId > GTopicIdInit) {
    url = CREATE_REAL_URL(self.interfaceURL, @"update.json");
    parameters = @{ @"pro_uid" : @(userModel.uid),
                    @"pro_topic_id" : @(topicId),
                    @"topic_name" : topicName,
                    @"time_length" : timeLength,
                    @"topic_desc" : topicDesc,
                    @"topic_price" : topicPrice,
    };
  } else {
    url = CREATE_REAL_URL(self.interfaceURL, @"new.json");
    parameters = @{ @"pro_uid" : @(userModel.uid),
                    @"topic_name" : topicName,
                    @"time_length" : timeLength,
                    @"topic_desc" : topicDesc,
                    @"topic_price" : topicPrice,
                    @"pro_user_location_name" : @"",
                    @"user_pro_topic_index" : @"",
                    @"user_pro_topic_index2" : @"",
    };
  }

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//删除话题
- (void)deleteTopicWithTopicId:(NSInteger)topicId
                    completion:(completion_t)completion
                         error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"remove.json");
  UserModel *userModel = [[CacheManager shareInstance] getLastLoginAccount];

  NSDictionary *parameters = @{ @"pro_uid" : @(userModel.uid),
                                @"consult_id" : @(topicId),
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

@end
